/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ddd.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Eric
 */
public class SQL2Form {

    private Connection con = null;
    private String host = "localhost";
    private String dbname = "ddd";
    private String tablename = "news";
    private String username = "root";
    private String password = "123456";
    private String condition = "";
    private String condition_value = "";

    public static void main(String[] args) {
        try {
            new SQL2Form().generate();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(SQL2Form.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(SQL2Form.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void generate() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://" + host + "/" + dbname + "?user=" + username + "&password=" + password + "&useUnicode=true&characterEncoding=UTF-8");

        Statement st = con.createStatement();
        String sql_la = "" ;
        if(condition!=null&&!"".equals(condition)&&!"null".equals(condition)){
            sql_la = "SELECT * FROM " + tablename + " WHERE "+ condition+ " LIKE \"%" + condition_value+"%\"";
        }else{            
            sql_la = "SELECT * FROM " + tablename;
        }
        ResultSet rs = st.executeQuery(sql_la);
        ResultSetMetaData md = rs.getMetaData();
        int col = md.getColumnCount();
        System.out.println("Number of Column : " + col);
        System.out.println("Columns Name: ");

        for (int i = 1; i <= col; i++) {
            String colname = md.getColumnName(i);
            System.out.println("String " + colname + " = request.getParameter(\"" + colname + "\");");
        }

        System.out.println("================================================");

        for (int i = 1; i <= col; i++) {
            String colname = md.getColumnName(i);
            System.out.println("System.out.println(" + colname + ");");
        }


        System.out.println("================================================");

        for (int i = 1; i <= col; i++) {
            String colname = md.getColumnName(i);
            System.out.println(tablename + ".set" + capitalizeFirstLetters(colname) + "(" + colname + ");");
        }

        System.out.println("================================================");
        
        for (int i = 1; i <= col; i++) {
            String colname = md.getColumnName(i);
            System.out.println("<tr>\n<td>" + colname + "</td>\n<td><input type=\"text\" id=\"" + colname +"\" name=\"" + colname + "\" value=\"<%=%>\"></td>\n</tr>");
        }

        con.close();
    }

    public static String capitalizeFirstLetters(String s) {
        for (int i = 0; i < s.length(); i++) {
            if (i == 0) {
                // Capitalize the first letter of the string.
                s = String.format("%s%s",
                        Character.toUpperCase(s.charAt(0)),
                        s.substring(1));
            }

            // Is this character a non-letter or non-digit?  If so
            // then this is probably a word boundary so let's capitalize
            // the next character in the sequence.
            if (!Character.isLetterOrDigit(s.charAt(i))) {
                if (i + 1 < s.length()) {
                    s = String.format("%s%s%s",
                            s.subSequence(0, i + 1),
                            Character.toUpperCase(s.charAt(i + 1)),
                            s.substring(i + 2));
                }
            }
        }
        return s;
    }
}
